$(function(){
	//上传图片
    upload("insHotelRoomsTable");
    addInsHotelRoomsTableImgComps();
});

function addInsHotelRoomsTableImgComps() {
    //定义变量，判断是新增还是修改,0是新增,1是修改
    //添加图片
    $('#insHotelRoomsTableContent').on('click', '.add_img', function () {
        var num = $(this).parent().find('.upload_div').length;
        var type = Number($(this).attr('data-type'));

        if (type == 0) {
            num += 1;
            var ht = '<div class="upload_div" style="float: left; margin-right: 15px;"><img id="myImageShow' + num + '" src="" width="100px" height="100px" /><input type="hidden" id="imgUrl' + num + '" name="banner" value=""/><input type="file" id="uploadPhotoFile' + num + '" name="photoFile" class="upload_file" data-type="' + type + '"></div>';
            $(this).before(ht);
            if ($(this).parent().find('.upload_div').length >= 6) {
                $(this).hide();
            }
        } else if (type == 1) {
            $('#add_standard').val('');
            $('#standardName').val('');
            $('#standardValues').val('');
            isModifyType = 0;
            $('#standard_Modal').modal();
        }
        //doc 结构改变了，要重新注册事件
        //上传图片
        upload("insHotelRoomsTable");
    });

    //删除照片
    $('#insHotelRoomsTableContent').on('click', '.del_img', function () {
        //第三张图消失
        $('.col-md-5 div').last().remove();
        //加号出来
        $('.add_img').show();
        if ($('.col-md-5 div').length == 0) {
            //减号消失
            $('.jian_img').css('display', 'none');
        }
    });
}

// 通过ID获取单条数据
function getDataByIdInInsHotelRoomsEdit(url, tmpl, content) {
    var tId = getUrlParam("id");
    if (!tId) {
        myAlert('id参数不能为空');
        var contentHtml = $(tmpl).render("");
        $(content).html(contentHtml);
        return;
    }
    $('#institutionId').val(tId);
    var ue;
    $.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data: {
            modelId: tId
        },
        success: function (data) {
            console.log(data);
            if (data.code == 1) {
                var contentHtml = $(tmpl).render(data.object);
                $(content).html(contentHtml);
                ue = UE.getEditor('hotelRooms_Brief');
                ue.addListener("ready", function () {
                    // editor准备好之后才可以使用
                    ue.setContent(data.object.hotelRooms_Brief);
                });
                
                //加载介绍图
                if (data.object.banner) {
                    var desImg = "";
                    $.each(data.object.banner.split(","), function (i) {
                        desImg += '<div class="upload_div" style="float: left; margin-right: 15px;"><img id="myImageShow' + (i + 1) + '" src="../../' + this + '" width="100px" height="100px" /><input type="hidden" id="imgUrl' + (i + 1) + '" name="banner" value="' + this + '"/><input type="file" id="uploadPhotoFile' + (i + 1) + '" name="photoFile" class="upload_file" data-type="' + (i + 1) + '"></div>';
                    });
                    $(content).find('.add_img').eq(0).before(desImg);
                    if (data.object.banner.split(",").length >= 6) {
                        $(content).find('.add_img').eq(0).hide();
                    }
                }
            } else {
                //异常
                var contentHtml = $(tmpl).render("");
                $(content).html(contentHtml);
                ue = UE.getEditor('hotelRooms_Brief');
                ue.addListener("ready", function () {
                    // editor准备好之后才可以使用
                    ue.setContent("");
                });
            }

            if (tId <= 0) {
                $("#isSellByCard_div").show();
                $("#hotelRooms_Price_unit_div").show();
            } else {
                $("#isSellByCard_div").hide();
                if (data.object.isSellByCard) {//按卡销售
                    $("#hotelRooms_Price_unit_div").hide();
                } else  {
                    $("#hotelRooms_Price_unit_div").show();
                }
            }
            //添加change事件
            $("#isSellByCard").change(function () {
                var _isSellByCard = $(this).val();
                if (_isSellByCard == 1) {
                    $("#bookPriceDiv").show();
                    $("#bookPrice").attr("data_rule", "");
                    $("#hotelRooms_Price_unit_div").hide();
                } else {
                    $("#bookPriceDiv").hide();
                    $("#bookPrice").attr("data_rule", "");
                    $("#hotelRooms_Price_unit_div").show();
                }
            });
        }
    });
}

//保存数据
function saveInsHotelRooms(url, backUrl) {
    $('#submit_form').isValid(function (result) {
        if (!result) {
            myAlert("还有不符合规定的字段填写，请检查！");
            return;
        }
        //轮播图
        var _imgurls = [];
        $('#insHotelRoomsTableContent').find('#imgUrls_div').find('input[name="imgUrl"]').each(function () {
            _imgurls.push($(this).val());
        });
        if (_imgurls.length <= 0) {
            myAlert('房间图片不能为空');
            return;
        }
        $("#saveOrupdate").addClass('disable_btn');
        $.ajax({
            url: url,
            type: "POST",
            timeout: 7000,
            data : $("#submit_form").serialize(),
            success: function (data) {
                if (data.code == 1) {
                    backAction(backUrl);
                } else {
                    $("#saveOrupdate").removeClass('disable_btn');
                    myAlert(data.message || '数据加载失败，稍后重试！');
                }
            },
            error: function () {
                $("#saveOrupdate").removeClass('disable_btn');
            }
        });
    });
}

